package com.yj.ccs.abs.web.assetData.service;

import com.yj.ccs.abs.boot.pagination.Page;
import com.yj.ccs.abs.web.assetData.dto.AreaAnalysisDto;
import com.yj.ccs.abs.web.assetData.dto.ConcentrationAnalysisDto;
import com.yj.ccs.abs.web.assetData.dto.PeriodDto;
import com.yj.ccs.abs.web.assetData.entity.*;
import com.yj.ccs.abs.web.assetData.mapper.AssetDataMapper;
import com.yj.ccs.abs.web.assetData.mapper.AssetDataPageMapper;
import com.yj.ccs.abs.web.assetData.mapper.PeriodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zhengguanyong on 2018/3/28.
 */
@Service
public class AssetDataService {

    @Autowired
    AssetDataMapper assetDataMapper;

    @Autowired
    AssetDataPageMapper assetDataPageMapper;

    @Autowired
    PeriodsMapper periodsMapper;

    public List<Periods> getAssetPeriods() {
        return periodsMapper.getAssetPeriods();
    }

    //资产池数据
    public AssetDataStat assetDataStatTotal() {
        AssetDataStat assetDataStat = new AssetDataStat();
        assetDataStat.setExistenceContract(assetDataMapper.countExistenceContract());
        int buyer = assetDataMapper.countExistenceContractBuyer();
        assetDataStat.setBuyer(buyer);
        int countBaseAsset = assetDataMapper.countBaseAsset();
        assetDataStat.setCountBaseAsset(countBaseAsset);
        if (buyer != 0) {
            assetDataStat.setAverageAsset(new BigDecimal(countBaseAsset).divide(new BigDecimal(buyer),2,BigDecimal.ROUND_HALF_UP));
        } else {
            assetDataStat.setAverageAsset(new BigDecimal(0));
        }
        BigDecimal sumAssetMoney = assetDataMapper.sumAssetMoney();
        assetDataStat.setSumAssetMoney(sumAssetMoney);
        if (countBaseAsset != 0) {
            assetDataStat.setAveAssetMoney(sumAssetMoney.divide(new BigDecimal(countBaseAsset),2,BigDecimal.ROUND_HALF_UP));
        } else {
            assetDataStat.setAveAssetMoney(new BigDecimal(0));
        }
        BigDecimal sumAssetAvailableMoney = assetDataMapper.sumAssetAvailableMoney();
        assetDataStat.setSumAssetAvailableMoney(sumAssetAvailableMoney);
        if (buyer != 0) {
            assetDataStat.setAveAssetAvailableMoney(sumAssetAvailableMoney.divide(new BigDecimal(buyer),2,BigDecimal.ROUND_HALF_UP));
        } else {
            assetDataStat.setAveAssetAvailableMoney(new BigDecimal(0));
        }
        BigDecimal sumOverdueAssetMoney = assetDataMapper.sumOverdueAssetMoney();
        assetDataStat.setSumOverdueAssetMoney(sumOverdueAssetMoney);
        if (sumAssetAvailableMoney.compareTo(new BigDecimal(0)) > 0) {
            assetDataStat.setOverdueRate(sumOverdueAssetMoney.multiply(new BigDecimal(100)).divide(sumAssetAvailableMoney,2,BigDecimal.ROUND_HALF_UP));
        } else {
            assetDataStat.setOverdueRate(new BigDecimal(0));
        }
        BigDecimal totalAmount = new BigDecimal(1000000000);  //融资总规模
        BigDecimal sumUnusedAssetMoney = totalAmount.subtract(sumAssetAvailableMoney);
        assetDataStat.setSumUnusedAssetMoney(sumUnusedAssetMoney);
        assetDataStat.setUnusedRate(sumUnusedAssetMoney.multiply(new BigDecimal(100)).divide(totalAmount,2,BigDecimal.ROUND_HALF_UP));

        return assetDataStat;
    }

    public List<AssetTop5> assetTop5List() {
        return assetDataMapper.assetTop5List();
    }

    public BillAnalysis calcRate(BillAnalysis billAnalysis,BillAnalysis forBillAnalysis) {
        if (forBillAnalysis.getSum_money_sum().compareTo(BigDecimal.ZERO) > 0) {
            billAnalysis.setSum_money_sum_rate(billAnalysis.getSum_money_sum().multiply(new BigDecimal(100)).divide(forBillAnalysis.getSum_money_sum(),2,BigDecimal.ROUND_HALF_UP));
        }
        if (forBillAnalysis.getSum_available_money().compareTo(BigDecimal.ZERO) > 0) {
            billAnalysis.setSum_available_money_rate(billAnalysis.getSum_available_money().multiply(new BigDecimal(100)).divide(forBillAnalysis.getSum_available_money(),2,BigDecimal.ROUND_HALF_UP));
        }
        if (forBillAnalysis.getAsset_count() > 0) {
            billAnalysis.setAsset_count_rate(new BigDecimal(billAnalysis.getAsset_count()).multiply(new BigDecimal(100)).divide(new BigDecimal(forBillAnalysis.getAsset_count()),2,BigDecimal.ROUND_HALF_UP));
        }
        if (billAnalysis.getAsset_count() > 0) {
            billAnalysis.setAve_available_money(billAnalysis.getSum_available_money().divide(new BigDecimal(billAnalysis.getAsset_count()),2,BigDecimal.ROUND_HALF_UP));
        }
        return billAnalysis;
    }

    public List<BillAnalysis> billAnalysisList() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        int[][] days_range = {{91,1000},{61,90},{31,60},{0,30},{-30,-1},{-60,-31},{-90,-61},{-1000,-91}};
        String [] remain_days = {"大于90天","61-90天","31-60天","小于30天","逾期0-30天","逾期31-60天","逾期61-90天","逾期大于90天"};
        BillAnalysis forBillAnalysis = assetDataMapper.forBillAnalysis();
        for (int i = 0; i < days_range.length; i++) {
            BillAnalysis billAnalysis = assetDataMapper.billAnalysis(days_range[i][0],days_range[i][1]);
            billAnalysis.setRemain_days(remain_days[i]);
            billAnalysisList.add(calcRate(billAnalysis,forBillAnalysis));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> billCycleList() {
        List<BillAnalysis> billCycleList = new ArrayList<>();
        int[][] days_range = {{0,30},{31,60},{61,90},{90,1000}};
        String [] credit_days = {"30天以内","31-60天","61-90天","90天以上"};
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        for (int i = 0; i < days_range.length; i++) {
            BillAnalysis billCycle = assetDataMapper.billCycle(days_range[i][0],days_range[i][1]);
            billCycle.setCredit_days(credit_days[i]);
            billCycleList.add(calcRate(billCycle,forAnalysisTotal));
        }
        return billCycleList;
    }

    public Page<BillAnalysis> concentrationAnalysisPage(ConcentrationAnalysisDto concentrationAnalysisDto) {
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        Page<BillAnalysis> page = null;
        switch (concentrationAnalysisDto.getType()) {
            case 1:page = assetDataPageMapper.buyerAnalysisPage(concentrationAnalysisDto);break;
            case 2:page = assetDataPageMapper.sellerAnalysisPage(concentrationAnalysisDto);break;
            case 3:page = assetDataPageMapper.parentAnalysisPage(concentrationAnalysisDto);break;
            case 4:page = assetDataPageMapper.finalParentAnalysisPage(concentrationAnalysisDto);break;
        }
        List<BillAnalysis> sellerAnalysisList = page.getPageResult();
        for (BillAnalysis billAnalysis: sellerAnalysisList) {
            billAnalysis = calcRate(billAnalysis,forAnalysisTotal);
        }
        return page;
    }

    public Page<BillAnalysis> areaAnalysisPage(AreaAnalysisDto areaAnalysisDto) {
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        Page<BillAnalysis> page = null;
        switch (areaAnalysisDto.getType()) {
            case 1:page = assetDataPageMapper.areaAnalysisPage(areaAnalysisDto);break;
        }
        List<BillAnalysis> sellerAnalysisList = page.getPageResult();
        for (BillAnalysis billAnalysis: sellerAnalysisList) {
            billAnalysis = calcRate(billAnalysis,forAnalysisTotal);
        }
        return page;
    }

    public List<BillAnalysis> shadowAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        String [] shadow_name = {"AAA","AA+","AA","AA-","A+","A","A-"};
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.shadowAnalysis();
        for (BillAnalysis ba: list) {
            ba.setShadow_name(shadow_name[ba.getShadow_rating()-1]);
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> industryAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.industryAnalysis();
        for (BillAnalysis ba: list) {
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> natureAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        String [] nature_name = {"国企","民企","国企(上市)","民企(上市)"};
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.natureAnalysis();
        for (BillAnalysis ba: list) {
            ba.setNature_name(nature_name[ba.getNature()-1]);
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> usageAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.usageAnalysis();
        for (BillAnalysis ba: list) {
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> goodsTypeAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.goodsTypeAnalysis();
        for (BillAnalysis ba: list) {
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> transportTypeAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        String [] transport_type_name = {"汽运","火运","船运"};
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.transportTypeAnalysis();
        for (BillAnalysis ba: list) {
            ba.setTransport_type_name(transport_type_name[ba.getTransport_type()-1]);
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> levelAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        String [] array_data = {"正常","逾期","关注","次级","违约"};
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.levelAnalysis();
        for (BillAnalysis ba: list) {
            ba.setLevel_name(array_data[ba.getLevel()-1]);
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> guaranteeAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        String [] array_data = {"无","信用","一般保证","最高额保证","一般抵押","最高额抵押","质押"};
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.guaranteeAnalysis();
        for (BillAnalysis ba: list) {
            ba.setGuarantee_name(array_data[ba.getGuarantee()-1]);
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> isSettledAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        String [] array_data = {"否","是"};
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.isSettledAnalysis();
        for (BillAnalysis ba: list) {
            ba.setIs_settled_name(array_data[ba.getIs_settled()]);
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public List<BillAnalysis> accusedAnalysis() {
        List<BillAnalysis> billAnalysisList = new ArrayList<>();
        String [] array_data = {"否","是"};
        BillAnalysis forAnalysisTotal = assetDataMapper.forAnalysisTotal();
        List<BillAnalysis> list = assetDataMapper.accusedAnalysis();
        for (BillAnalysis ba: list) {
            ba.setAccused_name(array_data[ba.getAccused()]);
            billAnalysisList.add(calcRate(ba,forAnalysisTotal));
        }
        return billAnalysisList;
    }

    public Page<AssetRecycle> assetRecyclePage(PeriodDto periodDto) {
        return assetDataPageMapper.assetRecyclePage(periodDto);
    }

    public Page<AssetRecycle> assetBuyingPage(PeriodDto periodDto) {
        return assetDataPageMapper.assetBuyingPage(periodDto);
    }




    public HashMap<String,Integer> pageToLimit(int page) {
        HashMap<String,Integer> am = new HashMap();
        if (page < 1) {page = 1;}
        int limit = page*10;
        int offset = (page-1)*10;
        am.put("limit",limit);
        am.put("offset",offset);
        return am;
    }

}
